﻿// -----------------------------------------------------------------------
// <copyright file="DmDichVuDataAccess.cs" company="">
// TODO: Copyright by dangppsoft@gmail.com.
// </copyright>
// -----------------------------------------------------------------------
namespace ManagerClinic.Core.DataAccess
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using ManagerClinic.Core.Common;
    using System.Data.Common;
    using System.Data;
    using Microsoft.Practices.EnterpriseLibrary.Data;
    using Microsoft.Practices.EnterpriseLibrary.Common;
    using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
    using ManagerClinic.Ultilities.Ultility;

    public class DmDichVuDataAccess
    {
        public bool InsertDmDichVu(DmDichVuEntity obj)
        {
            try
            {
                string sql = "spDmDichVu_Insert";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                db.AddInParameter(cmd, "MaDichVu", DbType.String, obj.MaDichVu);
                db.AddInParameter(cmd, "TenDichVu", DbType.String, obj.TenDichVu);
                db.AddInParameter(cmd, "MoTa", DbType.String, obj.MoTa);
                db.AddInParameter(cmd, "MaLoaiDichVu", DbType.String, obj.MaLoaiDichVu);
                db.AddInParameter(cmd, "TrangThaiSuDung", DbType.Boolean, obj.TrangThaiSuDung);
                int result = (int)db.ExecuteNonQuery(cmd);
                return (result > 0) ? true : false;
            }
            catch (Exception ex) { LogExceptionUltilities.LogException("InsertDmDichVu", ex.Message); throw ex; }
        }

        public bool EditDmDichVu(DmDichVuEntity obj)
        {
            try
            {
                string sql = "spDmDichVu_Update";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                db.AddInParameter(cmd, "MaDichVu", DbType.String, obj.MaDichVu);
                db.AddInParameter(cmd, "TenDichVu", DbType.String, obj.TenDichVu);
                db.AddInParameter(cmd, "MoTa", DbType.String, obj.MoTa);
                db.AddInParameter(cmd, "MaLoaiDichVu", DbType.String, obj.MaLoaiDichVu);
                db.AddInParameter(cmd, "TrangThaiSuDung", DbType.Boolean, obj.TrangThaiSuDung);
                int result = (int)db.ExecuteNonQuery(cmd);
                return (result > 0) ? true : false;
            }
            catch (Exception ex) { LogExceptionUltilities.LogException("UpdateDmDichVu", ex.Message); throw ex; }
        }

        public bool DeleteDmDichVu(string maDichVu)
        {
            try
            {
                string sql = "spDmDichVu_DeleteByID";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                db.AddInParameter(cmd, "MaDichVu", DbType.String, maDichVu);
                int result = (int)db.ExecuteNonQuery(cmd);
                return (result > 0) ? true : false;
            }
            catch (Exception ex) { LogExceptionUltilities.LogException("DeleteDmDichVu", ex.Message); throw ex; }
        }

        public DmDichVuEntity GetDmDichVu(string maDichVu)
        {
            try
            {
                DmDichVuEntity obj = null;
                string sql = "spDmDichVu_SelectByID";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                db.AddInParameter(cmd, "MaDichVu", DbType.String, maDichVu);
                using (IDataReader reader = db.ExecuteReader(cmd))
                {
                    while (reader.Read())
                    {
                        obj = new DmDichVuEntity();
                        obj.MaDichVu = reader["MaDichVu"].ToString();
                        obj.TenDichVu = reader["TenDichVu"].ToString();
                        obj.MoTa = reader["MoTa"].ToString();
                        obj.MaLoaiDichVu = reader["MaLoaiDichVu"].ToString();
                        obj.TrangThaiSuDung = String.IsNullOrEmpty(reader["TrangThaiSuDung"].ToString()) == true ? false : (bool)reader["TrangThaiSuDung"];
                    }
                }
                return obj;
            }
            catch (Exception ex) { LogExceptionUltilities.LogException("GetDmDichVu", ex.Message); throw ex; }
        }

        public List<DmDichVuEntity> GetDmDichVuList()
        {
            try
            {
                List<DmDichVuEntity> list = new List<DmDichVuEntity>();
                DmDichVuEntity obj = null;
                string sql = "spDmDichVu_SelectAll";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                using (IDataReader reader = db.ExecuteReader(cmd))
                {
                    while (reader.Read())
                    {
                        obj = new DmDichVuEntity();
                        obj.MaDichVu = reader["MaDichVu"].ToString();
                        obj.TenDichVu = reader["TenDichVu"].ToString();
                        obj.MoTa = reader["MoTa"].ToString();
                        obj.MaLoaiDichVu = reader["MaLoaiDichVu"].ToString();
                        obj.TrangThaiSuDung = String.IsNullOrEmpty(reader["TrangThaiSuDung"].ToString()) == true ? false : (bool)reader["TrangThaiSuDung"];
                        list.Add(obj);
                    }
                }
                return list;
            }
            catch (Exception ex) { LogExceptionUltilities.LogException("GetDmDichVuList", ex.Message); throw ex; }
        }

    }
}